﻿using System;
using System.Collections;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Xml;
using System.Diagnostics;
using System.Linq;
using System.Xml.Linq;

/// <summary>
/// Summary description for EventWS
/// </summary>
[WebService(Namespace = "http://novakov.miecio.net/ServerDashboard/Event")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class EventWS : System.Web.Services.WebService {

    public EventWS () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    private void AddElement(XmlNode node, string name, string value)
    {
        XmlElement element = node.OwnerDocument.CreateElement(name);
        element.InnerXml = value;
        node.AppendChild(element);
    }

    [WebMethod]
    public string GetEntry(string index,string log) 
    {       
        EventLog ev = new EventLog(log);        
        int i = Int32.Parse(index);

        var res = from EventLogEntry entry in ev.Entries
                  where entry.Index == i
                  select new XElement("event",
                      new XElement("source", entry.Source),
                      new XElement("message", new XCData(entry.Message)),
                      new XElement("type", entry.EntryType.ToString()),
                      new XElement("time", entry.TimeWritten.ToString()),
                      new XElement("user", entry.UserName != null ? entry.UserName : "(none)"),
                      new XElement("category", entry.Category),
                      new XElement("computer", entry.MachineName));
        System.Threading.Thread.Sleep(5000);

        return res.First().ToString();
    }
    
}

